//
//  LastView.m
//  MMLOOK
//
//  Created by dllo on 15/9/28.
//  Copyright © 2015年 武俊娥. All rights reserved.
//

#import "LastView.h"

#import "WedModel.h"
#import "UIImageView+WebCache.h"
#import "RecommendCollectionViewCell.h"

#import "JudgeConnection.h"

#define RealWidth ([UIScreen mainScreen].bounds.size.width - 40) / 3

@interface LastView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain)UILabel *label;
@property(nonatomic, retain)UICollectionView *collect;
@property(nonatomic, retain)NSMutableArray *arr;



@end

@implementation LastView


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.arr = [NSMutableArray array];
        
        self.label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, frame.size.width - 10, 40)];
        [self addSubview:_label];
        [_label release];

        //推荐
        [self creatCollectionView];
        
    }
    
    return self;
}


-(void)creatCollectionView
{
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    
    flow.itemSize = CGSizeMake(RealWidth, RealWidth);
    
    flow.minimumInteritemSpacing = 0;
    
    flow.minimumLineSpacing = 10;
    
    self.collect = [[UICollectionView alloc]initWithFrame:CGRectMake(10, 40, self.frame.size.width - 20, self.frame.size.height - 50) collectionViewLayout:flow];
    
    self.collect.delegate = self;
    
    self.collect.dataSource = self;
    
    self.collect.backgroundColor = [UIColor whiteColor];
    
    [self.collect registerClass:[RecommendCollectionViewCell class] forCellWithReuseIdentifier:@"recommendCollectCell"];
    
    [self addSubview:self.collect];
    
    [_collect release];

}


-(void)sendMessage:(NSArray *)arr
{
    self.label.text = @"精品推荐";
    
    self.label.textColor = [UIColor orangeColor];
  
    self.arr = [NSMutableArray arrayWithArray:arr];
    
    //刷新数据
    [self.collect reloadData];

}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    if (self.arr.count > 6) {
        
        return 6;
    }
    
    return self.arr.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    RecommendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"recommendCollectCell" forIndexPath:indexPath];
    
    cell.model = [self.arr objectAtIndex:indexPath.item];
    
    return cell;
    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([JudgeConnection judgeConnection]) {
        
      self.sendIdBlock([self.arr objectAtIndex:indexPath.item]);
        
    }
}


-(void)pushDetailWithSendIdBlock:(void(^)(WedModel *model))block
{
    self.sendIdBlock = block;
}


@end
